package com.cxzl.pcxt.service;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.cxzl.pcxt.dao.UserDao;
import com.cxzl.pcxt.entity.User;

/**
 * 用户服务
 * 
 * @author DMJ
 *
 */
@Service("pxctUserService")
@Transactional
public class UserService {

	@Resource(name = "pcxtUserDao")
	private UserDao userDao;

	/**
	 * 添加用户
	 * 
	 * @param user
	 */
	public void addUser(User user) {
		Assert.notNull(user, "用户不能为空");
		Assert.hasText(user.getMoblie(), "手机号不能为空");
		Assert.hasText(user.getPassword(), "密码不能为空");
		String md5Pass = DigestUtils.md5Hex(user.getPassword());
		user.setPassword(md5Pass);
		userDao.saveEntity(user);
	}

	/**
	 * 用户登录
	 * 
	 * @param mobile
	 * @param password
	 * @return
	 */
	public User login(String mobile, String password) {
		User user = userDao.findByMobile(mobile);
		Assert.notNull(user, "用户名和密码不正确");
		String md5Pass = DigestUtils.md5Hex(password);
		Assert.isTrue(StringUtils.equals(md5Pass, user.getPassword()),
				"用户名和密码不正确");
		return user;
	}

}
